package cn.itcast.nems.listener.order;

import cn.hutool.core.lang.Assert;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.listener.NemsMessageListener;
import cn.itcast.nems.order.dto.message.OrderCreatedMessageDTO;
import cn.itcast.nems.studentrecord.dto.change.OrderCreatedDTO;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordSingupService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
/**
 * <p>
 * 订单创建消息监听处理逻辑
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/7 2:07 PM
 **/
@Component
@RequiredArgsConstructor
public class OrderCreatedMessageListener implements NemsMessageListener {
    private final static String TagName  =  "TAG_NEMS_ORDER_CREATED" ;
    private final StudentRecordSingupService studentRecordSingupService ;

    @Override
    public String getTagName() {
        return TagName;
    }

    @Override
    public synchronized void processMessage(String body) {
        OrderCreatedMessageDTO createdMessageDTO = JSON.parseObject(body, OrderCreatedMessageDTO.class);
        Assert.notNull(createdMessageDTO, "【消息监听】tag: TAG_NEMS_ORDER_CREATED,消息体中业务数据实体为空");
        Assert.notBlank(createdMessageDTO.getOrderId(), "【消息监听】tag: TAG_NEMS_ORDER_CREATED,业务订单ID为空");
        TenantInfoHolder.setTenantInfo(new TenantInfo(createdMessageDTO.getTenantId(), createdMessageDTO.getSubtenantId()));
        // 生成学籍 并维护缴费情况中数量
        OrderCreatedDTO orderCreatedDTO = new OrderCreatedDTO();
        BeanUtils.copyProperties(createdMessageDTO, orderCreatedDTO);
        orderCreatedDTO.setOrderCreatedDatetime(createdMessageDTO.getCreatedDatetime());
        studentRecordSingupService.singup(orderCreatedDTO);
    }
}
